package com.epoint.utils.template.insert;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.epoint.utils.JDBCUtil;
import com.epoint.utils.SQLUtil;

/**
 * 我的新增模板
 * @author 梁卓
 *
 * @param <T> 泛型
 */
public class MySqlInsert<T> extends InsertTemplate<T>
{
    private Connection con = null;
    private PreparedStatement ps = null;
    private int i = 0;

    @Override
    void getConnection() {
        con = JDBCUtil.getConnection();
        
    }

    /**
         * 执行sql返回结果
     * @param sql sql
     * @param obj 要新增的对象信息
     * @return 结果
     */
    @Override
    int insertData(String sql, T obj) {
        try {
            ps = con.prepareStatement(sql);
            ps = SQLUtil.insertObjectInfo(obj, ps);
            System.out.println("MySqlInsert->" + ps);
            i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("新增模板错误！");
        }
        return i;
    }

    @Override
    void closeConnection() {
        try {
            JDBCUtil.closeConnection(null, ps, con);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("新增模板连接关闭失败！");
        }
    }

    
}
